package com.my.commonlibrary.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.my.commonlibrary.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Author : Cooper
 * Time : 2016/9/26  11:41
 * Description : Tab+ViewPager基类 需要实现initViewpagerFragmentList方法，添加子fragment和title
 */

public abstract class BaseTabViewPagerFragment extends Fragment {

    @Bind(R.id.mTabLayout)
    TabLayout mTabLayout;
    @Bind(R.id.mViewPager)
    ViewPager mViewPager;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList= new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.base_frag_tab_viewpager_layout, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViewpagerFragmentList(mFragmentList,mTitleList);
        BaseViewPagerAdapter baseViewPagerAdapter = new BaseViewPagerAdapter(getFragmentManager(),mFragmentList,mTitleList);
        mViewPager.setAdapter(baseViewPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /**
     * 初始化fragment和title 添加进来实例
     * @param fragmentList
     * @param titleList
     */
    protected abstract void initViewpagerFragmentList(List<Fragment> fragmentList, List<String> titleList);
}
